﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Moq;
using NUnit.Framework;
using SportsStore.ServicesInterfaces;
using SportsStore.Web.Controllers;

namespace SportsStore.UnitTests.Controllers
{
  [TestFixture]
  public class CartItemControllerTest : BaseControllerTests
  {
    private Mock<ICartItemService> _cartItemService;
    private string _sessionValue = "cartId";

    [SetUp]
    public void Setup()
    {
      _cartItemService = new Mock<ICartItemService>();
    }

    [Test]
    public void GivenACartItemController_WhenICallItsAddToCartMethod_TheCorrectViewIsReturned()
    {
      var controller = new CartItemController(_cartItemService.Object, null);
      _cartItemService.Setup(c => c.AddItem(It.IsAny<int>(), It.IsAny<string>(), 1));
      SetControllerContext(controller);

      MockHttpContext.SetupGet(x => x.Session["CartId"]).Returns(_sessionValue);

      var result = controller.AddToCart(1) as RedirectToRouteResult;

      Assert.That(result, Is.Not.Null);
      Assert.That(result.RouteValues["action"], Is.EqualTo("index").IgnoreCase);
    }

    [Test]
    public void GivenACartItemController_WhenICallItsRemoveFromCartMethod_TheCorrectViewIsReturned()
    {
      var controller = new CartItemController(_cartItemService.Object, null);
      _cartItemService.Setup(c => c.RemoveItem(It.IsAny<int>(), It.IsAny<string>()));
      SetControllerContext(controller);

      MockHttpContext.SetupGet(x => x.Session["CartId"]).Returns(_sessionValue);

      var result = controller.AddToCart(1) as RedirectToRouteResult;

      Assert.That(result, Is.Not.Null);
      Assert.That(result.RouteValues["action"], Is.EqualTo("index").IgnoreCase);
    }
  }
}
